package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"path/filepath"
	"strings"
	"time"
)

/*
 * 遍历文件夹下所有的文件信息
 * 并进行对比
 */
func TraverseFolder() {
	new_files := getFileStructure()
	old_files := getFileLogStructure()

}

func WriteToFileLog() {
	files := getFileStructure()

	f, err := os.Create("file_log.txt")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	buffer := bufio.NewWriter(f)

	for _, line := range files {
		currentTime := time.Now().Unix()
		_, err := buffer.WriteString(string(currentTime) + "|" + line + "\n")
		if err != nil {
			log.Fatal(err)
		}
	}

	if err := buffer.Flush(); err != nil {
		log.Fatal(err)
	}
}

func getFileStructure() []string {
	var files []string
	root := ReadConfig().FileConfig.FolderPath
	err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
		files = append(files, path)
		return nil
	})
	if err != nil {
		fmt.Println(err)
	}

	return files
}

func getFileLogStructure() []string {
	files, err := os.ReadFile("file_log.txt")
	if err != nil {
		log.Fatal(err)
	}

	return strings.Split(string(files), "\n")
}
